package com.hnqcgc.dao.impl;

import com.hnqcgc.dao.BaseDao;
import com.hnqcgc.dao.IAdminDao;
import com.hnqcgc.db.MyTransaction;
import com.hnqcgc.vo.TAdmin;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/***
 * 管理员Dao实现类
 */
public class AdminDaolmpl extends BaseDao implements IAdminDao {

    private MyTransaction myTransaction;
    public AdminDaolmpl(MyTransaction myTransaction){
        super(myTransaction);
        this.myTransaction=myTransaction;
    }
    @Override
    public TAdmin login(String adminName, String adminPassword) throws SQLException {
        //1:sql语句
        String sql="select * from t_admin where admin_name =? and admin_password= ?";

        TAdmin admin=new TAdmin();

        QueryRunner queryRunner=new QueryRunner();

        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);

        admin= queryRunner.query(myTransaction.getConn(),sql,
                new BeanHandler<TAdmin>(TAdmin.class,rowProcessor),adminName,adminPassword);

        //myTransaction.closeConn();
        return admin;

    }

    @Override
    public List<TAdmin> queryAll() throws SQLException {
        String sql="select * from t_admin";

        QueryRunner queryRunner=new QueryRunner();
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);

        return queryRunner.query(myTransaction.getConn(),sql,new BeanListHandler<TAdmin>(TAdmin.class,rowProcessor));

    }

    @Override
    public List<TAdmin> queryByNameLike(String adminName) throws SQLException {
        String sql="select * from t_admin where admin_name like?";
        QueryRunner queryRunner=new QueryRunner();
        GenerousBeanProcessor beanProcessor=new GenerousBeanProcessor();
        RowProcessor rowProcessor=new BasicRowProcessor(beanProcessor);

        return queryRunner.query(myTransaction.getConn(),sql, new BeanListHandler<TAdmin>(TAdmin.class,rowProcessor),"%"+adminName+"%");
    }

    @Override
    public int insert(TAdmin admin) {
        //1:sql语句
        String sql="insert into t_admin values(null,?,?,?,?,?)";
        //2：调用BaseDao统一增删改，updat方法
        return this.update(sql,admin.getAdminName(),admin.getAdminPassword(),admin.getAdminRole(),admin.getPhone(),admin.getEmail());
    }

    @Override
    public int delete(long adminId) {
        String sql="delete from t_admin where admin_id=?";
        return this.update(sql,adminId);
    }

    @Override
    public int update(TAdmin admin) {
        String sql="update t_admin set admin_name=?, admin_password=?,admin_role=?,phone=?,email=? where admin_id=?";
        return this.update(sql,admin.getAdminName(),admin.getAdminPassword(),admin.getAdminRole(),admin
        .getPhone(),admin.getEmail(),admin.getAdminId());
    }
}
